package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.HaiweidaSynLogJdbc;
import com.wuseguang.springcloud.dao.repository.HaiweidaSynLogRepository;
import com.wuseguang.springcloud.entity.HaiweidaSynLog;
import com.wuseguang.springcloud.service.HaiweidaSynLogService;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 海威达数据同步日志 服务实现类
 * </p>
 *
 * @author
 * @since 2020-10-29
 */
@Service
public class HaiweidaSynLogServiceImpl implements HaiweidaSynLogService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HaiweidaSynLogRepository haiweidaSynLogRepository;
    @Autowired
    private HaiweidaSynLogJdbc haiweidaSynLogJdbc;


    @Override
    public Result inserthaiweidaSynLog(String uuid, String logUrl, String logSuccessUrl) {
        logger.info("海威达同步结果");
        HaiweidaSynLog log = haiweidaSynLogRepository.inserthaiweidaSynLog(uuid);
        log.setLogUrl(logUrl);
        return new Result();
    }


    @Override
    public Result findhaiweidaSynLog(String schoolId, Integer indexNo, Integer rows) {
        logger.info("海威达同步结果");
        List<HaiweidaSynLog> log = haiweidaSynLogRepository.findBySchoolId(schoolId);
        return new Result(log);
    }

    @Override
    public Result findhaiweidaLog(String schoolId, String startTime, String endTime, Integer indexNo, Integer rows) {
        //按时间查询日志
        String sql = "select id,dates,DATE_FORMAT(add_time,'%Y-%m-%d %T') add_time,school_id  from `haiweida_log` where school_id = ? and  add_time between DATE_FORMAT(?,'%Y-%m-%d %T') and DATE_FORMAT(?,'%Y-%m-%d %T') ORDER BY id desc";
        List<Map<String,Object>> list = haiweidaSynLogJdbc.queryListMap(sql,schoolId,startTime,endTime);

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(),rows, tool.getList(), indexNo));
    }
}
